package com.xujianlun.cpms.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xujianlun.cpms.entity.sys.User;
import com.xujianlun.cpms.exception.ServiceException;
import com.xujianlun.cpms.mapper.TAccessMapper;
import com.xujianlun.cpms.utils.TokenUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xujianlun.cpms.common.Response;

import com.xujianlun.cpms.service.ITAccessService;
import com.xujianlun.cpms.entity.TAccess;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xujianlun
 * @since 2022-12-21
 */
@RestController
@RequestMapping("/t-access")
public class TAccessController {

    @Resource
    private ITAccessService tAccessService;
    @Resource
    private TAccessMapper accessMapper;
    // 新增或者更新
    @PostMapping
    public Response save(@RequestBody TAccess tAccess) {
        //获取当前用户的userid
       if (tAccess.getId()==null){
           Integer userId = TokenUtil.getTokenUserId();
//           Boolean check = tAccessService.check(userId);
//           if(check==true){
//               throw new ServiceException(-200,"今日已经提交");
//           }
           tAccess.setUserId(userId);
           tAccess.setTime(DateUtil.now());
           tAccessService.save(tAccess);
           return Response.success();
       }
       tAccessService.updateById(tAccess);
        return Response.success();
    }
    @DeleteMapping("/{id}")
    public Response delete(@PathVariable Integer id) {
        tAccessService.removeById(id);
        return Response.success();
    }

    @PostMapping("/del/batch")
    public Response deleteBatch(@RequestBody List<Integer> ids) {
        tAccessService.removeByIds(ids);
        return Response.success();
    }

    @GetMapping
    public Response findAll() {
        return Response.success(tAccessService.list());
    }

    @GetMapping("/{id}")
    public Response findOne(@PathVariable Integer id) {
        return Response.success(tAccessService.getById(id));
    }

    @GetMapping("/page")
    public Response findPage(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize) {
        QueryWrapper<TAccess> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Response.success(tAccessService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @GetMapping("/page/school")
    public Response findSchoolPage(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize,
                                   @RequestParam(defaultValue = "") String name,
                                   @RequestParam(defaultValue = "") String localName) {
        String local="学校";
        return Response.success(accessMapper.findSchoolPage(new Page<>(pageNum, pageSize), name,local,localName));
    }

    @GetMapping("/page/community")
    public Response findcoPage(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize,
                                   @RequestParam(defaultValue = "") String name,
                               @RequestParam(defaultValue = "") String localName) {
        String local="社区";
        return Response.success(accessMapper.findSchoolPage(new Page<>(pageNum, pageSize), name,local,localName));
    }

    @GetMapping("/page/company")
    public Response findcanPage(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize,
                                   @RequestParam(defaultValue = "") String name,
                                @RequestParam(defaultValue = "") String localName) {
        String local="企业";
        return Response.success(accessMapper.findSchoolPage(new Page<>(pageNum, pageSize), name,local,localName));
    }
}

